<template>
  <div class="ChildPage TablePage" v-loading="loading">
    <vb-page ref="page">
      <template #headerBtn="scope">
        <el-button size="small" icon="el-icon-plus" type="primary" @click="$refs.certificateDetailDialog.open()">新增</el-button>
      </template>
      <template #table-handle="scope">
        <el-button type="text" icon="el-icon-edit" size="small" @click="$refs.certificateDetailDialog.open(scope.row)">编辑</el-button>
        <el-button type="text" icon="el-icon-delete" @click="doDelete(scope.row)" size="small" style="color: red">删除</el-button>
      </template>
    </vb-page>
  </div>
  <certificate-detail-dialog ref="certificateDetailDialog" @save="doSave"></certificate-detail-dialog>
</template>

<script>
import certificateDetailDialog from "../traceCode/certificateDetailDialog.vue";

export default {
  data() {
    return {
      materialLoading: false,
      reloadMaterial: false,
    }
  },
  components: {
    certificateDetailDialog
  },
  async mounted() {
    await this.getStock()
  },
  methods: {
    doSave() {
      this.$refs.page.reload()
    },
    async doDelete(row) {
      try {
        await this.$confirm(`确定删除吗`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
      } catch(e) {
        return
      }
      this.loading = true
      try {
        let res = await this.callApi('delete_certificate', { id: row.id })
        if (!res.res) {
          return
        }
        this.$message.success(`已删除`)
        this.$refs.page.reload()
      } finally {
        this.loading = false
      }
    },
    async getStock() {
      this.loading = true;
      try {
        let res = await this.$api.getChildDictionaryListByKeys({
          keys: ['stock']
        });
        if (!res.res) return;
        this.stockList = res.stock;
      } finally {
        this.loading = false;
      }
    },
    /**
     * 获取状态名
     */
    getStockName(val) {
      if (this.stockList && this.stockList.length > 0) {
        for(let i = 0; i < this.stockList.length; i++) {
          if (this.stockList[i].key === val) {
            return this.stockList[i].name;
          }
        }
      }
      return '';
    },
  }
}
</script>

<style scoped>

</style>
